from models import CMAParams
from exts import db

# 查询所有cma参数
def get_cma_params():
    cma_params = CMAParams.query.all()
    return cma_params


# 通过id查询cma参数
def get_cma_params_by_id(id):
    cma_params = CMAParams.query.filter_by(id=id).first()
    return cma_params


# 通过类别查询cma参数
def get_cma_params_by_category(category):
    cma_params = CMAParams.query.filter(CMAParams.category.like(f'%{category}%')).all()
    return cma_params


# 通过类别和、项目名称查询数据
def get_cma_params_by_category_and_parameterName(category=None, parameterName=None, standardMethod=None):
    query = CMAParams.query  # 创建一个查询对象
    if category:
        query = query.filter(CMAParams.category.like(f'%{category}%'))
    if parameterName:
        query = query.filter(CMAParams.parameterName.like(f'%{parameterName}%'))
    if standardMethod:
        query = query.filter(CMAParams.standardMethod.like(f'%{standardMethod}%'))
    cma_params = query.all()
    return cma_params


# 增加CMA
def add_cma_params(cma_params):
    db.session.add(cma_params)
    db.session.commit()
    return cma_params